//
//  HNSH_AddWithdrawalAccountViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/6/14.
//

#import "HNSH_AddWithdrawalAccountViewController.h"

@interface HNSH_AddWithdrawalAccountViewController ()

/* nameField */
@property (nonatomic, strong) UITextField *nameField;
/* zfbField */
@property (nonatomic, strong) UITextField *zfbField;
/* bankNameField */
@property (nonatomic, strong) UITextField *bankNameField;

@end

@implementation HNSH_AddWithdrawalAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.type == 1) {
        [self showNavTitle:@"绑定支付宝" showLeftItem:YES];
    } else {
        [self showNavTitle:@"绑定银行卡" showLeftItem:YES];
    }
    
    [self addAllViews];
}

- (void)addAllViews {
    self.nameField = [self configCellWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, 50) title:@"姓名：" placeholder:@"请输入姓名"];
    self.nameField.text = self.infoModel.name;
    [[self.nameField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField *textField = x;
        NSLog(@"输入名字 ==> %@",textField.text);
    }];
    
    if (self.type == 1) {
        self.zfbField = [self configCellWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight+50, kScreenWidth, 50) title:@"支付宝：" placeholder:@"请输入支付宝账户"];
        self.zfbField.text = self.infoModel.account;
        [[self.zfbField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UITextField *textField = x;
            NSLog(@"支付宝： ==> %@",textField.text);
        }];
    } else {
        self.zfbField = [self configCellWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight+50, kScreenWidth, 50) title:@"银行卡：" placeholder:@"请输入银行卡号"];
        self.zfbField.text = self.infoModel.account;
        [[self.zfbField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UITextField *textField = x;
            NSLog(@"银行卡： ==> %@",textField.text);
        }];
        
        self.bankNameField = [self configCellWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight+100, kScreenWidth, 50) title:@"开户行：" placeholder:@"请输入开户行"];
        self.bankNameField.text = self.infoModel.additional;
        [[self.bankNameField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UITextField *textField = x;
            NSLog(@"开户行： ==> %@",textField.text);
        }];
    }
    
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    submitButton.frame = CGRectMake(16, kNavBarAndStatusBarHeight+240, kScreenWidth-32, 50);
    [submitButton setTitle:@"保存" forState:UIControlStateNormal];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    submitButton.backgroundColor = [UIColor appGreenColor];
    [self.view addSubview:submitButton];
    [self.view clipCornerWithView:submitButton cornerWidth:25 cornerType:UIRectCornerAllCorners];
    __weak typeof(self) weakSelf = self;
    [[submitButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf loadUpLoadInfoData];
    }];
    
}

- (UITextField *)configCellWithFrame:(CGRect)frame
                               title:(NSString *)title
                         placeholder:(NSString *)placeholder {
    UIView *bgView = [[UIView alloc]initWithFrame:frame];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(16, 14, 100, 22)];
    titleLable.text = title;
    titleLable.textColor = [UIColor colorWithHexString:@"#333333"];
    titleLable.font = [UIFont systemFontOfSize:16];
    [bgView addSubview:titleLable];
    
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(kScreenWidth-216, 0, 200, bgView.height-1)];
    textField.textColor = [UIColor colorWithHexString:@"#333333"];
    textField.font = [UIFont systemFontOfSize:16];
    NSMutableDictionary * attDic = [@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"], NSFontAttributeName:textField.font} mutableCopy];
    NSMutableAttributedString * attPlace = [[NSMutableAttributedString alloc] initWithString:placeholder attributes:attDic];
    textField.attributedPlaceholder = attPlace;
    textField.textAlignment = NSTextAlignmentRight;
    [bgView addSubview:textField];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, bgView.height-1, kScreenWidth, 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    [bgView addSubview:line];
    
    return textField;
}

- (void)loadUpLoadInfoData{
    [HNSH_UserNetServer requestUserWithdrawAccountUpdateWithAccount:self.zfbField.text name:self.nameField.text additional:self.bankNameField.text type:self.type success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [kSTWindow makeToast:@"更新成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}


@end
